package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.BillBaseDubboService;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.enums.OrderTypeEnum;
import com.niiwoo.civet.mobile.service.bill.UserBillInvestOrderService;
import com.niiwoo.civet.mobile.vo.response.bill.JumpTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageResponseDTO;
import com.niiwoo.civet.trade.service.creditor.CreditorRightsDubboService;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceInvestDubboService;
import com.niiwoo.civet.trade.service.project.ProjectDubboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 投资账单说情
 * Created by Yijian on 2018/11/8.
 */
@Slf4j
@Service
public class InvestBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private ProjectDubboService projectDubboService;
    @Reference(version = "1.0.0")
    private IntelligenceInvestDubboService intelligenceInvestDubboService;
    @Reference(version = "1.0.0")
    private BillBaseDubboService billBaseDubboService;
    @Reference(version = "1.0.0")
    private CreditorRightsDubboService creditorRightsDubboService;
    @Autowired
    private UserBillInvestOrderService projectDetailService;

    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        //不显示资金明细项
        response.setAmountItems(new ArrayList<>());

        //查询是否有退款记录
        List<UserBillBaseResponseDTO> backBillBaseList = billBaseDubboService.queryBackSourceUserBillBases(userBillBase.getUserId(), userBillBase.getBillBaseId());
        if (!CollectionUtils.isEmpty(backBillBaseList)) {
            for (UserBillBaseResponseDTO backBillBase : backBillBaseList) {
                UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
                specialItem.setCode("refundRecord");//前端需要用来做颜色标识
                specialItem.setName("退款记录");
                specialItem.setContent("已退款 " + backBillBase.getAmount());
                specialItem.setIsNeedJump(Boolean.TRUE);
                specialItem.setJumpType(JumpTypeEnum.BILL_DETAIL.getCode());
                specialItem.setJumpParamOne(backBillBase.getBillBaseId().toString());
                response.getSpecialItems().add(specialItem);
            }
        }

        //组装投资订单
        String investOrderContent = projectDetailService.getInvestOrderContent(userBillBase,response);
        if(OrderTypeEnum.ZNT_INVEST.getCode().equals(userBillBase.getOrderType())){
            UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
            specialItem.setName("投资订单");
            specialItem.setContent(investOrderContent);
            specialItem.setIsNeedJump(Boolean.TRUE);
            specialItem.setJumpType(JumpTypeEnum.ZNT_INVEST_ORDER.getCode());
            specialItem.setJumpParamOne(userBillBase.getOrderId());
            response.getSpecialItems().add(specialItem);
        }else {
            UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
            specialItem.setName("投资订单");
            specialItem.setContent(investOrderContent);
            specialItem.setIsNeedJump(Boolean.TRUE);
            specialItem.setJumpType(JumpTypeEnum.INVEST_ORDER.getCode());
            specialItem.setJumpParamOne(userBillBase.getOrderId());
            response.getSpecialItems().add(specialItem);
        }

        //订单编号
        UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
        specialItem.setName("订单编号");
        specialItem.setContent(response.getOrderId());
        response.getSpecialItems().add(specialItem);
        return JSON.toJSONString(response);
    }


    //查询智能投信息
    private ProjectPackageResponseDTO queryProjectPackageInfo(Long packageId) {
        ProjectPackageResponseDTO projectPackageResponseDTO = intelligenceInvestDubboService.getIntelligenceInvestInfo(packageId);
        return projectPackageResponseDTO;
    }


}
